package cn.anecansaitin.inet.mixins;

import cn.anecansaitin.inet.TranslateHelper;
import mcjty.lib.varia.ItemStackList;
import mcjty.rftoolsbase.api.xnet.gui.IEditorGui;
import mcjty.rftoolsbase.api.xnet.helper.AbstractConnectorSettings;
import mcjty.xnet.apiimpl.items.ItemConnectorSettings;
import net.minecraft.core.Direction;
import org.jetbrains.annotations.NotNull;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;

@Mixin(ItemConnectorSettings.class)
public abstract class ItemConnectorSettingMixin extends AbstractConnectorSettings {
    public ItemConnectorSettingMixin(@NotNull Direction side) {
        super(side);
    }

    @Shadow
    private ItemConnectorSettings.ItemMode itemMode;
    @Shadow
    private ItemConnectorSettings.ExtractMode extractMode;
    @Shadow
    private int speed;
    @Shadow
    private ItemConnectorSettings.StackMode stackMode;
    @Shadow
    private boolean tagsMode;
    @Shadow
    private boolean metaMode;
    @Shadow
    private boolean nbtMode;
    @Shadow
    private boolean blacklist;
    @Shadow
    private Integer priority;
    @Shadow
    private Integer count;
    @Shadow
    private Integer extractAmount;
    @Final
    @Shadow
    private ItemStackList filters;

    @Shadow
    protected abstract String getFilterIndexString();

    @Override
    public void createGui(IEditorGui gui) {
        advanced = gui.isAdvanced();
        String[] speeds;
        if (advanced) {
            speeds = new String[]{"5", "10", "20", "60", "100", "200"};
        } else {
            speeds = new String[]{"10", "20", "60", "100", "200"};
        }

        sideGui(gui);
        colorsGui(gui);
        redstoneGui(gui);
        gui.nl()
                .choices(ItemConnectorSettings.TAG_MODE, TranslateHelper.getText("inet.controller.setting.button.io.tooltip"), itemMode, ItemConnectorSettings.ItemMode.values())
                .shift(5)
                .choices(ItemConnectorSettings.TAG_STACK, TranslateHelper.getText("inet.controller.setting.button.item_stack.tooltip"), stackMode, ItemConnectorSettings.StackMode.values());

        if (stackMode == ItemConnectorSettings.StackMode.COUNT && itemMode == ItemConnectorSettings.ItemMode.EXT) {
            gui.integer(ItemConnectorSettings.TAG_EXTRACT_AMOUNT, TranslateHelper.getText("inet.controller.setting.label.item_stack.tooltip"), extractAmount, 30, 64);
        }

        gui
                .shift(10)
                .choices(ItemConnectorSettings.TAG_SPEED, TranslateHelper.getText("inet.controller.setting.button.speed.tooltip1"), Integer.toString(speed * 5), speeds)
                .nl();

        gui
                .label(TranslateHelper.getText("inet.controller.setting.label.pri.text")).integer(ItemConnectorSettings.TAG_PRIORITY, TranslateHelper.getText("inet.controller.setting.label.pri.tooltip"), priority, 36).shift(5)
                .label("#")
                .integer(ItemConnectorSettings.TAG_COUNT, itemMode == ItemConnectorSettings.ItemMode.EXT ? TranslateHelper.getText("inet.controller.setting.label.min.item.tooltip") : TranslateHelper.getText("inet.controller.setting.label.max.item.tooltip"), count, 30);

        if (itemMode == ItemConnectorSettings.ItemMode.EXT) {
            gui
                    .shift(5)
                    .choices(ItemConnectorSettings.TAG_EXTRACT, TranslateHelper.getText("inet.controller.setting.button.item_extract.tooltip"), extractMode, ItemConnectorSettings.ExtractMode.values());
        }

        gui
                .nl()

                .toggleText(ItemConnectorSettings.TAG_BLACKLIST, TranslateHelper.getText("inet.controller.setting.button.blacklist.tooltip"), TranslateHelper.getText("inet.controller.setting.button.blacklist.name"), blacklist).shift(0)
                .toggleText(ItemConnectorSettings.TAG_TAGS, TranslateHelper.getText("inet.controller.setting.button.tag.tooltip"), TranslateHelper.getText("inet.controller.setting.button.tag.name"), tagsMode).shift(0)
                .toggleText(ItemConnectorSettings.TAG_META, TranslateHelper.getText("inet.controller.setting.button.meta.tooltip"), TranslateHelper.getText("inet.controller.setting.button.meta.name"), metaMode).shift(0)
                .toggleText(ItemConnectorSettings.TAG_NBT, TranslateHelper.getText("inet.controller.setting.button.nbt.tooltip"), TranslateHelper.getText("inet.controller.setting.button.nbt.name"), nbtMode).shift(0)
                .choices(ItemConnectorSettings.TAG_FILTER_IDX, TranslateHelper.getText("inet.controller.setting.button.filter.tooltip"), getFilterIndexString(), "<Off>", "1", "2", "3", "4")
                .nl();
        for (int i = 0; i < ItemConnectorSettings.FILTER_SIZE; i++) {
            gui.ghostSlot(ItemConnectorSettings.TAG_FILTER + i, filters.get(i));
        }
    }
}
